package com.ruoyi.web.controller.zhhq;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zhhq.domain.Building;
import com.ruoyi.zhhq.service.IBuildingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 楼Controller
 * 
 * @author zsl
 * @date 2022-10-30
 */
@RestController
@RequestMapping("/zhhq/building")
public class BuildingController extends BaseController
{
    @Autowired
    private IBuildingService buildingService;

    /**
     * 查询楼列表
     */
    @PreAuthorize("@ss.hasPermi('zhhq:building:list')")
    @GetMapping("/list")
    public TableDataInfo list(Building building)
    {
        startPage();
        List<Building> list = buildingService.selectBuildingList(building);
        return getDataTable(list);
    }
    
    /**
     * 查询楼列表
     */
    @PreAuthorize("@ss.hasPermi('zhhq:building:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll()
    {
        List<Building> list = buildingService.selectBuildingListAll();
        return getDataTable(list);
    }
    
    /**
     * 导出楼列表
     */
    @PreAuthorize("@ss.hasPermi('zhhq:building:export')")
    @Log(title = "楼", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Building building)
    {
        List<Building> list = buildingService.selectBuildingList(building);
        ExcelUtil<Building> util = new ExcelUtil<Building>(Building.class);
        util.exportExcel(response, list, "楼数据");
    }

    /**
     * 获取楼详细信息
     */
    @PreAuthorize("@ss.hasPermi('zhhq:building:query')")
    @GetMapping(value = "/{buildingId}")
    public AjaxResult getInfo(@PathVariable("buildingId") String buildingId)
    {
        return AjaxResult.success(buildingService.selectBuildingByBuildingId(buildingId));
    }

    /**
     * 新增楼
     */
    @PreAuthorize("@ss.hasPermi('zhhq:building:add')")
    @Log(title = "楼", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Building building)
    {
    	if(buildingService.selectBuildingByBuildingId(building.getBuildingId())!=null) {
    		return AjaxResult.error("楼id已存在，不能重复添加！");
    	}
    	if(buildingService.selectBuildingByBuildingName(building.getBuildingName())!=null) {
    		return AjaxResult.error("楼名称已存在，不能重复添加！");
    	}
    	building.setCreateBy(getUsername());
        return toAjax(buildingService.insertBuilding(building));
    }

    /**
     * 修改楼
     */
    @PreAuthorize("@ss.hasPermi('zhhq:building:edit')")
    @Log(title = "楼", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Building building)
    {
        return toAjax(buildingService.updateBuilding(building));
    }

    /**
     * 删除楼
     */
    @PreAuthorize("@ss.hasPermi('zhhq:building:remove')")
    @Log(title = "楼", businessType = BusinessType.DELETE)
	@DeleteMapping("/{buildingIds}")
    public AjaxResult remove(@PathVariable String[] buildingIds)
    {
        return toAjax(buildingService.deleteBuildingByBuildingIds(buildingIds));
    }
}
